#!/usr/local/bin/python2.7

import difflib, time
from constants import *

class ConversionLog(object):
    
    def __init__(self):
        self._clean_pass_entries = []
        self._dirty_pass_entries = []
        self._failed_entries = []

    def isCurrent(self, new_path):
        indexed = self._clean_pass_entries
        indexed += self._dirty_pass_entries
        matcher = difflib.SequenceMatcher(None)

        for entry in indexed:
            logged_path = entry.src
            matcher.set_seqs(logged_path, new_path)
            match = matcher.find_longest_match(0, len(logged_path), \
                                               0, len(new_path))
            if abs(match.size - len(new_path)) <= 4:
                if entry.time > 0:
                    if entry.time >= os.stat(new_path).st_mtime:
                        return True
        return False

    def addEntry(self, log_entry):
        if log_entry.passed_clean:
            for ent in list(self._clean_pass_entries):
                if log_entry.src == ent.src:
                    self._clean_pass_entries.remove(ent)
            self._clean_pass_entries.append(log_entry)
        elif log_entry.passed_dirty:
            for ent in list(self._dirty_pass_entries):
                if log_entry.src == ent.src:
                    self._dirty_pass_entries.remove(ent)
            self._dirty_pass_entries.append(log_entry)
        elif log_entry.failed:
            for ent in list(self._failed_entries):
                if log_entry.src == ent.src:
                    self._failed_entries.remove(ent)
            self._failed_entries.append(log_entry)
        
    def getCsv(self):
        pass
    csv = property(getCsv)

    def getRst(self):
        head1 = 'Clean Pass Entries'
        head2 = 'Dirty Pass Entries'
        head3 = 'Failed Entries'

        und1 = '\n' + '=' * len(head1) + '\n\n'
        und2 = '\n' + '=' * len(head2) + '\n\n'
        und3 = '\n' + '=' * len(head3) + '\n\n'

        ent1 = self._makeRstEntries(self._clean_pass_entries)
        ent2 = self._makeRstEntries(self._dirty_pass_entries)
        ent3 = self._makeRstEntries(self._failed_entries)

        rst = '\n\n'
        rst += head1 + und1 + ent1 + '\n\n'
        rst += head2 + und2 + ent2 + '\n\n'
        rst += head3 + und3 + ent3 + '\n\n'

        return rst
    rst = property(getRst)

    def _makeRstEntries(self, entries):
        rst = ''
        for entry in entries:
            rst += entry.rst + '\n'
        return rst

    def __str__(self):
        mystr = ''
        mystr += '\n-------- Clean Pass Entries --------\n'
        mystr += self._makeEntryStrings(self._clean_pass_entries)
        mystr += '\n-------- Dirty Pass Entries --------\n'
        mystr += self._makeEntryStrings(self._dirty_pass_entries)
        mystr += '\n---------- Failed Entries ----------\n'
        mystr += self._makeEntryStrings(self._failed_entries)
        return mystr

    def _makeEntryStrings(self, entries):
        mystr = ''
        for entry in entries:
            mystr += str(entry) + '\n'
        return mystr

class LogEntry(object):

    def __init__(self):
        self._time = 0
        self._src_filepath = ''
        self.parsed_successfully = False
        self._dst_filepath = ''
        self._src_absolute = ''
        self._errors = []

    def addError(self, err):
        self._errors.append(err)

    def addErrors(self, errs):
        self._errors.extend(errs)

    def getPassedClean(self):
        if self.parsed_successfully:
            if len(self._errors) == 0:
                return True
        return False
    passed_clean = property(getPassedClean)

    def getPassedDirty(self):
        if self.parsed_successfully:
            if len(self._errors) > 0:
                return True
        return False
    passed_dirty = property(getPassedDirty)

    def getFailed(self):
        if self._dst_filepath == '':
            return True
        return False
    failed = property(getFailed)

    def getSrc(self):
        return self._src_filepath
    def setSrc(self, val):
        self._src_filepath = val
    src = property(getSrc, setSrc)

    def getTime(self):
        return self._time
    def setTime(self, val):
        self._time = val
    time = property(getTime, setTime)

    def getSrc2(self):
        return self._src_absolute
    def setSrc2(self, val):
        self._src_absolute = val 
    src_abs = property(getSrc2, setSrc2)

    def getDst(self):
        return self._dst_filepath
    def setDst(self, val):
        self._dst_filepath = val
    dst = property(getDst, setDst)

    def getCsv(self):
        passcurr_filepath
    csv = property(getCsv)

    def getRst(self):
        src_rel = self._src_filepath.replace('\\', '/')
        heading = src_rel[2:]
        underline = '-' * len(heading)

        src_abs = self._src_absolute.replace('\\', '/')
        src_abs = src_abs.replace(' ', '%20')
        src = ':Source Filepath: <file:/' + src_abs + '>'
        dst_abs = self._dst_filepath.replace('\\', '/')
        dst_abs = dst_abs.replace(' ', '%20')
        dst = ':Destination Filepath: <file:///' + dst_abs + '>'

        tme = ''
        try:
            tme = ':Time: ' + str(self._time)
        except:
            pass

        errs = ':Errors:\n'
        for err in self._errors:
            errs += '    ' + '* ' + str(err) + '\n'

        rst = heading + '\n' + underline + '\n\n' + src + '\n' + \
              dst + '\n' + tme + '\n' + errs

        return rst

    rst = property(getRst)

    def __str__(self):
        mystr = 'Entry('
        mystr += 'src=' + self._src_filepath + ', '
        mystr += 'dst=' + self._dst_filepath + ', '
        mystr += 'time=' + self._time + ', '
        mystr += 'errors=' + str(len(self._errors))
        mystr += ')'
        return mystr
        
